//
//  PhotosViewController.m
//  Mecca
//
//  Created by Ryan Enguero on 6/14/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "PhotosViewController.h"

#import "CustomCollectionCell.h"

#import "Photos.h"

#import "ParserClass.h"

@interface PhotosViewController ()

@end

@implementation PhotosViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
      //  self.title = NSLocalizedString(@"Photos", @"Photos");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.navigationController.navigationBar setHidden:YES];
   
    [self.collectionView registerNib:[UINib nibWithNibName:@"CustomCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];
    
    if ([[[ParserClass sharedItems] photos] count] == 0) {
        UIAlertView *noPhotos = [[UIAlertView alloc]initWithTitle:@"" message:@"No Photos" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [noPhotos show];
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"photosTabClicked.png"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[[ParserClass sharedItems] photos] count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (CustomCollectionCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    Photos *photo = [[ParserClass sharedItems] photos][indexPath.row];
    
    cell.captionLbl.text = [NSString stringWithFormat:@"cell %li",(long)indexPath.row];
    
    [cell setImageString:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@", [photo image]]];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomCollectionCell *cell = (CustomCollectionCell*)[collectionView cellForItemAtIndexPath:indexPath];
    NSArray *views = [cell.contentView subviews];
    UILabel *label = [views objectAtIndex:0];
    NSLog(@"Select %@",label.text);
}


- (IBAction)backAction:(id)sender {
    
    [self.tabBarController.navigationController.navigationBar setHidden:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)viewDidUnload{
    
    [super viewDidUnload];
    
    [[ParserClass sharedItems] emptyPhotos];
}

@end
